from enum import Enum
from itertools import groupby


# 定义一个枚举类
class WarmLevel(Enum):
    high = ('高', 0)
    medium = ('中', 0)
    low = ('低', 0)

    @staticmethod
    def GetWarmEnum():
        warm_map = {}
        for level in WarmLevel:
            childMap = {'name': level.value[0], 'score': level.value[1], 'key': level.name}
            warm_map[level.name] = childMap
        return warm_map


def DoWarmMethod():
    warms = WarmLevel.GetWarmEnum()

    dataList = [{'level': 'low', 'name': 'chen', 'age': 20}, {'level': 'medium', 'name': 'hua', 'age': 20},
                {'level': 'high', 'name': 'xiao', 'age': 20}, {'level': 'low', 'name': 'mei', 'age': 32},
                {'level': 'high', 'name': 'es', 'age': 23}]
    # 按 level 对 dataList 进行排序
    dataList.sort(key=lambda x: x['level'])
    # 使用 groupby 进行分组
    groups = groupby(dataList, key=lambda x: x['level'])
    for level, group in groups:
        group = list(group)
        count = len(group)
        obj = warms.get(level)
        if obj is not None:
            obj['score'] = count
       # print(f"Level: {level}, 数量: {count}")
    lists = []
    for key in warms:
        lists.append(warms.get(key))
    print(lists)



if __name__ == "__main__":
    DoWarmMethod()
